package edu.gatech.stolen.tea;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;


/**
 * This class will hold the user accounts
 * @author Leshi
 *
 */
public class Accounts implements Serializable {
	/**
	 * 
	 */

	private  Map<String, String> accounts = new HashMap<String, String>();
	private  Map<String, User> users= new HashMap<String, User>();
	private static final long serialVersionUID = 1L;
	/**
	 * the database
	 */
	public ItemEditor itemEditor= new ItemEditor();

	/**
	 * the account holder that holds all user login information
	 */


	/**
	 * Creates a new user
	 * if the username already exist, the user will not be created
	 * @param username
	 * @param password
	 * @return
	 */
	 public boolean createUser(String username, String password){
		if (checkExistingUser(username)){
			return false;
		}else{
			accounts.put(username, password);
			User newUser = new User(username);
			users.put(username, newUser);
			itemEditor.addNewUser(newUser);
			//Log.d("CREATE USER", "itemEditor storage:" + ItemEditor.itemStorage);
			return true;
		}
	}
	/**
	 * check whether username already exist
	 * @param newUser
	 * @return true if already exist, false otherwise
	 */
	  public boolean checkExistingUser(String newUser) {
		if(accounts.containsKey(newUser)){
			return true;
		}else{
			return false;
		}
		
	}
	/**
	 * log in the user
	 * @param username
	 * @param password
	 * @return true if successful, false otherwise
	 */
	 public boolean login(String username, String password){
		if (checkExistingUser(username)){
			if(accounts.get(username).equals(password)){
				itemEditor.setCurrentUser(this.getCurrentUser(username));
				return true;
			}
		}
		return false;
	}
	/**
	 * 
	 * @param the current user name
	 * @return a corrisponding user object
	 */
	 public User getCurrentUser(String user){
		return users.get(user);
	}
	 /**
	  * saves the accounts class to the disk for permanance
     * @param path a string representation of the path to save the data to
	  */
	 public void saveData(String path){
		 try{
				ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File(path+"/accounts")));
				oos.writeObject(this);
				oos.flush();
				oos.close();
			}
			catch (Exception e) {
				e.printStackTrace();		
				}
	 }
	 /**
	  * fetches the saved account class
     * @param path  a string representation of the path to the loadable data is
	  * @return the account to be loaded
	  */
	 public Accounts loadData(String path){
		 Object o =null;
		 Log.v("trying to load"," trying to load data");
		 try{
				ObjectInputStream ois= new ObjectInputStream(new FileInputStream(
						new File(path+"/accounts")));
				o=ois.readObject();
			}
			catch (Exception e) {
				Log.v("loadData","failure to load");
				e.printStackTrace();		
				}
		 return (Accounts) o;
	 }
}
